//
//  WBHomeViewController.swift
//  Weibo
//
//  Created by wade on 17/2/17.
//  Copyright © 2017年 wade. All rights reserved.
//

import UIKit

private let cellid = "cellid"

class WBHomeViewController: WBBaseViewController {

    lazy var statusViewModel = WBStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        
        
    }
    
    override func loadData() {
        
            statusViewModel.loadStatus(pullup: self.isPullup, completion: { (isBool,isShouldRefresh) in
                
                self.refreshControl?.endRefreshing()
                
                self.isPullup = false
                
                if isShouldRefresh
                {
                    self.tableview?.reloadData()
                }
                
            }
        )
    }
    
    func showFriends()  {
        print(#function)
        
        navigationController?.pushViewController(WBDiscoverViewController.init(), animated: true)
        
    }
    

}
extension WBHomeViewController{

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusViewModel.statusList.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellid, for: indexPath)
        
        cell.textLabel?.text = statusViewModel.statusList[indexPath.row].text
        
        return cell
        
    }
    
}

extension WBHomeViewController{

    
    
    

    override func setupUI() {
        super.setupUI()
        
        tableview?.register(UITableViewCell.self, forCellReuseIdentifier: cellid)
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        setupNavTitle()
    }
    
    
    
    
    func setupNavTitle() {
        
        let button = UIButton.cz_textButton(WBNetworkManager.shared.userAccount.screen_name, fontSize: 17, normalColor: UIColor.darkGray, highlightedColor: UIColor.black)
        
        button?.setImage(UIImage(named: "navigationbar_arrow_down"), for: .normal)
        button?.setImage(UIImage(named: "navigationbar_arrow_up"), for: .selected)
        button?.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        button?.addTarget(self, action: #selector(titleBtnClick), for: .touchUpInside)
        
//        button?.titleLabel?.frame = (button?.titleLabel?.frame.offsetBy(dx: -(button?.imageView?.frame.width)!, dy: 0))!
//        button?.imageView?.frame = (button?.imageView?.frame.offsetBy(dx: (button?.titleLabel?.frame.width)!, dy: 0))!
        
        navigationItem.titleView = button
        
        
    }
    
    func titleBtnClick(b:UIButton) {
        b.isSelected = !b.isSelected
    }
    

}
